import cv2
import os
import numpy as np



def img2vector(imgfilename):
    # 以 灰度格式读入图片，返回一维向量
    img = cv2.imread(imgfilename,cv2.IMREAD_GRAYSCALE);
    rows , columns = img.shape
    vec = img.reshape(rows * columns)
    return vec

# 根据 t-72 主站坦克训练集的目录，获取该目录下所有（图像）文件名
t_72_training_dir = "../training/T-72"
imgfiles = os.listdir(t_72_training_dir)

# 初始化图像数据矩阵
N = len(imgfiles)
traingmatrix = np.zeros((N,128*128)) # 每个图像对应一行

for i in range(N):
    imgfilewithdir = t_72_training_dir + "\\" + imgfiles[i]
    v = img2vector(imgfilewithdir)
    traingmatrix[i] = v

# 输出矩阵
print(traingmatrix)

# 输出矩阵的行和列
print(traingmatrix.shape)






